/*
 * Created by JFormDesigner on Sun Oct 19 18:54:09 CST 2025
 */

package com.geek.javase.ch10.jformdesigntest.other;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;
import java.util.List;

/**
 * @author 27636
 */
public class MyTable extends JFrame {
    public static void main(String[] args) {
        new MyTable().setVisible(true);
    }
    public MyTable() {
        initComponents();
        //表格数据
        List<User> list = new ArrayList();
        list.add(new User("路人1",22));
        list.add(new User("路人2",20));
        list.add(new User("路人3",18));
        list.add(new User("路人4",30));
        list.add(new User("路人5",29));


        //表格标题
        String[] titles = {"编号","name","age"};
        //填充数据
        Object[][] data = new Object[list.size()][titles.length];
        for (int i = 0; i <data.length ; i++) {
            //给每一行填充数据
            data[i] = new Object[]{i+1, list.get(i).getName(), list.get(i).getAge()};
        }
        //封装模型数据
        DefaultTableModel model = new DefaultTableModel(data,titles);
        //设置数据
        table1.setModel(model);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    null, null, null
                }
            ));
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(55, 85, 235, 115);

        contentPane.setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
